#include "map.h"
void init_map(Map * map)
{
    int i;
    for(i = 0;i<MAP_MAX;i++){
        map->id[i] = -1;
        map->id_label[i] = NULL;
        map->id_num[i] = NULL;
        map->textview[i] = NULL;
        map->thread[i] = NULL;
        map->window[i] = NULL;
    }
}
void add_map_first(Map * map,int id, GtkWidget * id_label,GtkWidget * id_num)
{
    int i;
    for(i=0;i<MAP_MAX;i++){
        if(map->id[i] == -1){
            map->id[i] = id;
            map->id_label[i] = id_label;
            map->id_num[i] = id_num;
            return ;
        }
    }
}
void add_map_again(Map * map,int id, GtkWidget * window,GtkWidget * textview,GThread * thread)
{
    int i;
    for(i = 0;i<MAP_MAX; i++){
        if(map->id[i] == id){
            map->window[i] = window;
            map->textview[i] = textview;
            map->thread[i] = thread;
            return ;
        }
    }
}

GtkWidget *  id_to_idlabel(Map *map,int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id)
            return map->id_label[i];
    }
    return NULL;
}
int idlabel_to_id(Map *map, GtkWidget * id_label)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id_label[i] == id_label)
            return map->id[i];
    }
    return -1;
}

GtkWidget *  id_to_idnum(Map *map,int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id)
            return map->id_num[i];
    }
    return NULL;
}
int idnum_to_id(Map *map,GtkWidget * id_num)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id_num[i] == id_num)
            return map->id[i];
    }
    return -1;
}

GtkWidget *  id_to_window(Map *map,int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id)
            return map->window[i];
    }
    return NULL;
}
int window_to_id(Map *map, GtkWidget * window)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->window[i] == window)
            return map->id[i];
    }
    return -1;
}

GtkWidget *  id_to_textview(Map *map,int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id)
            return map->textview[i];
    }
    return NULL;
}
int textview_to_id(Map *map,GtkWidget * textview)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->textview[i] == textview)
            return map->id[i];
    }
    return -1;
}

GThread * id_to_thread(Map *map, int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id)
            return map->thread[i];
    }
    return NULL;
}
int thread_to_id(Map *map,GThread *thread)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->thread[i] == thread)
            return map->id[i];
    }
    return -1;
}

void dele_id_map(Map *map,int id)
{
    int i;
    for(i =0;i<MAP_MAX; i++){
        if(map->id[i] == id){
            map->id[i] = -1;
            map->id_label[i] = NULL;
            map->id_num[i] = NULL;
            map->textview[i] = NULL;
            map->thread[i] = NULL;
            map->window[i] = NULL;
            return ;
        }
    }
}
